<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>pseudorandom.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_testing_pseudorandom.js.html">pseudorandom.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2009 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview PseudoRandom provides a mechanism for generating deterministic
<a name="line17"></a> * psuedo random numbers based on a seed. Based on the Park-Miller algorithm.
<a name="line18"></a> * See http://www.cit.gu.edu.au/~anthony/info/C/RandomNumbers and
<a name="line19"></a> * http://www.erikoest.dk/rng.htm.
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.testing.PseudoRandom&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Class for unit testing code that uses Math.random. Generates deterministic
<a name="line30"></a> * random numbers.
<a name="line31"></a> *
<a name="line32"></a> * @param {number} opt_seed The seed to use.
<a name="line33"></a> * @param {boolean} opt_install Whether to install the PseudoRandom at
<a name="line34"></a> *     construction time.
<a name="line35"></a> * @extends {goog.Disposable}
<a name="line36"></a> * @constructor
<a name="line37"></a> */
<a name="line38"></a>goog.testing.PseudoRandom = function(opt_seed, opt_install) {
<a name="line39"></a>  goog.Disposable.call(this);
<a name="line40"></a>
<a name="line41"></a>  /**
<a name="line42"></a>   * The sequence of numbers to be returned by calls to random()
<a name="line43"></a>   * @type {number}
<a name="line44"></a>   * @private
<a name="line45"></a>   */
<a name="line46"></a>  this.seed_ = opt_seed ||
<a name="line47"></a>               goog.testing.PseudoRandom.seedUniquifier_++ + goog.now();
<a name="line48"></a>
<a name="line49"></a>  if (opt_install) {
<a name="line50"></a>    this.install();
<a name="line51"></a>  }
<a name="line52"></a>};
<a name="line53"></a>goog.inherits(goog.testing.PseudoRandom, goog.Disposable);
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Helps create a unique seed.
<a name="line57"></a> * @type {number}
<a name="line58"></a> * @private
<a name="line59"></a> */
<a name="line60"></a>goog.testing.PseudoRandom.seedUniquifier_ = 0;
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Constant used as part of the algorithm.
<a name="line65"></a> * @type {number}
<a name="line66"></a> */
<a name="line67"></a>goog.testing.PseudoRandom.A = 48271;
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Constant used as part of the algorithm. 2^31 - 1.
<a name="line72"></a> * @type {number}
<a name="line73"></a> */
<a name="line74"></a>goog.testing.PseudoRandom.M = 2147483647;
<a name="line75"></a>
<a name="line76"></a>
<a name="line77"></a>/**
<a name="line78"></a> * Constant used as part of the algorithm. It is equal to M / A.
<a name="line79"></a> * @type {number}
<a name="line80"></a> */
<a name="line81"></a>goog.testing.PseudoRandom.Q = 44488;
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Constant used as part of the algorithm. It is equal to M % A.
<a name="line86"></a> * @type {number}
<a name="line87"></a> */
<a name="line88"></a>goog.testing.PseudoRandom.R = 3399;
<a name="line89"></a>
<a name="line90"></a>
<a name="line91"></a>/**
<a name="line92"></a> * Constant used as part of the algorithm to get values between 0 and 1.
<a name="line93"></a> * @type {number}
<a name="line94"></a> */
<a name="line95"></a>goog.testing.PseudoRandom.ONE_OVER_M = 1.0 / goog.testing.PseudoRandom.M;
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Whether this PseudoRandom has been installed.
<a name="line100"></a> * @type {boolean}
<a name="line101"></a> * @private
<a name="line102"></a> */
<a name="line103"></a>goog.testing.PseudoRandom.prototype.installed_;
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * The original Math.random function.
<a name="line108"></a> * @type {function(): number}
<a name="line109"></a> * @private
<a name="line110"></a> */
<a name="line111"></a>goog.testing.PseudoRandom.prototype.mathRandom_;
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Installs this PseudoRandom as the system number generator.
<a name="line116"></a> */
<a name="line117"></a>goog.testing.PseudoRandom.prototype.install = function() {
<a name="line118"></a>  if (!this.installed_) {
<a name="line119"></a>    this.mathRandom_ = Math.random;
<a name="line120"></a>    Math.random = goog.bind(this.random, this);
<a name="line121"></a>    this.installed_ = true;
<a name="line122"></a>  }
<a name="line123"></a>};
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * Disposes of the MockRandom.
<a name="line128"></a> */
<a name="line129"></a>goog.testing.PseudoRandom.prototype.disposeInternal = function() {
<a name="line130"></a>  goog.testing.PseudoRandom.superClass_.disposeInternal.call(this);
<a name="line131"></a>  this.uninstall();
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * Uninstalls the PseudoRandom.
<a name="line137"></a> */
<a name="line138"></a>goog.testing.PseudoRandom.prototype.uninstall = function() {
<a name="line139"></a>  if (this.installed_) {
<a name="line140"></a>    Math.random = this.mathRandom_;
<a name="line141"></a>    this.installed_ = false;
<a name="line142"></a>  }
<a name="line143"></a>};
<a name="line144"></a>
<a name="line145"></a>
<a name="line146"></a>/**
<a name="line147"></a> * @return {number} The next number in the sequence.
<a name="line148"></a> */
<a name="line149"></a>goog.testing.PseudoRandom.prototype.random = function() {
<a name="line150"></a>  var hi = this.seed_ / goog.testing.PseudoRandom.Q;
<a name="line151"></a>  var lo = this.seed_ % goog.testing.PseudoRandom.Q;
<a name="line152"></a>  var test = goog.testing.PseudoRandom.A * lo -
<a name="line153"></a>             goog.testing.PseudoRandom.R * hi;
<a name="line154"></a>  if (test &gt; 0) {
<a name="line155"></a>    this.seed_ = test;
<a name="line156"></a>  } else {
<a name="line157"></a>    this.seed_ = test + goog.testing.PseudoRandom.M;
<a name="line158"></a>  }
<a name="line159"></a>  return this.seed_ * goog.testing.PseudoRandom.ONE_OVER_M;
<a name="line160"></a>};
</pre>


</body>
</html>
